const BehaviorAll = require("../../../behaviors/behavior_all.js");
const Tool = require("../../../utils/tool.js");
const ConstraintForm = require("../../../utils/constraint_form.js");
const Pay_wx = require("../../../utils/pay_wx.js");
const ServiceUser = require("../../../service/service_user.js");
const ServiceSys = require("../../../service/service_sys.js");

Component({
  /**
   * behaviors
   */
  behaviors: [BehaviorAll],
  /**
   * 页面的初始数据
   */
  data: { 
    /**
     * 操作方式：  提现 == 1, 列表 == 2
     */
    operateWay: 1,
    /** 用户全部积分 */
    score: 0,
    /** 提现金额 */
    withdrawValue: "",
    withdrawMinAmount: 0,
    withdrawOrderList:[],
    /** 账户数据 */
    account: null,
    /**
     * 开始页
     */
    startPage: 0
  },
  /**
   * 监听属性
   */
  observers: {
    'operateWay': function (v) {
      const _this = this;
      // 获取积分
      ServiceUser.getScore(function (data) {
        _this.setData({ score: data.score })
      });
      if(v===2){
        ServiceUser.listWithdrawOrder(0, function(data){
          _this.setData({ 
            startPage: 0,
            withdrawOrderList: data.records
          });
        });
      }
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      const _this = this;
      // 获取积分
      ServiceUser.getScore(function (data) {
        _this.setData({ score: data.score })
      });
      ServiceSys.get(function(data){
        _this.setData({ 
          withdrawMinAmount: data.withdrawMinAmount,
          withdrawValue: data.withdrawMinAmount
        });
      });
      ServiceUser.getAccount(function(data){
        _this.setData({ account: data });
      });
    },
    /**
     * 提现/记录 切换
     */
    onSwitch: function (e) {
      const _this = this;
      const way = Number(e.currentTarget.dataset.way);
      const operateWay = _this.data.operateWay;
      if (way !== operateWay){
        _this.setData({ operateWay: way});
      }
    },
    /**
     * 提现金额改变
     */
    onValueChange: function (e) {
      const _this = this;
      _this.setData({
        withdrawValue: ConstraintForm.toNumber(e, _this.data.withdrawValue)
      });
    },
    /**
     * 提现提交
     */
    onWithdrawSubmit: function () {
      const _this = this;
      const amount = _this.data.withdrawValue;
      // 验证
      _this._validateSubmit(
        function(){
          // 提现
          ServiceUser.submitWithdrawOrder(amount, function (data) {
            _this.setData({ operateWay: 2 });
          });
        },
        function(){
          ServiceUser.getAccount(function (data) {
            _this.setData({ account: data });
            _this._validateSubmit(
              function(){
                // 提现
                ServiceUser.submitWithdrawOrder(amount, function (data) {
                  _this.setData({ operateWay: 2 });
                });
              },
              function(){
                Tool.confirm("提现信息不全，点击确认去补全信息", function () {
                  wx.navigateTo({ url: '/model_user/pages/set/set', })
                });
              }
            );
          });
        }
      );
    },
    /**
     * 验证提交
     */
    _validateSubmit: function (success, fail){
      const _this = this;
      const amount = _this.data.withdrawValue;
      const minAmount = _this.data.withdrawMinAmount;
      const score = _this.data.score;
      if (amount < minAmount) {
        Tool.showInfo("最少提现金额为" + minAmount + "元");
        return;
      }
      if (amount > score) {
        Tool.showInfo("提现金额不能大于账户余额");
        return;
      }
      const account = _this.data.account;
      if (!account.name || !account.accountNumber || !account.bank || !account.phone) {
        fail();
      } else {
        success();
      }
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
      const _this = this;
      const operateWay = _this.data.operateWay;
      if (operateWay !== 2){
        return;
      }
      let startPage = _this.data.startPage + 1;
      ServiceUser.listWithdrawOrder(startPage, function (data) {
        _this.data.withdrawOrderList.push(...data.records);
        _this.setData({
          startPage: startPage,
          withdrawOrderList: _this.data.withdrawOrderList
        });
      });
    }

  }
})
